package com.xindaoapp.happypet.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.api.AddClient;
import com.xindaoapp.happypet.api.Address;
import com.xindaoapp.happypet.bean.UploadInfo;
import com.xindaoapp.happypet.utils.ProgressHUD;

public class FindPasswordActivity extends UMBaseActivity implements OnClickListener {
    @ViewInject(R.id.tittle_common_cancel)
    private ImageView tittle_common_cancel;
    @ViewInject(R.id.tittle_common_content)
    private TextView tittle_common_content;
    @ViewInject(R.id.findpsw_content)
    private EditText findpsw_content;
    @ViewInject(R.id.findpsw)
    private Button findpsw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_findpassword);
        ViewUtils.inject(this);
        initClick();
    }

    private void initClick() {
        tittle_common_content.setText("找回密码");
        tittle_common_cancel.setOnClickListener(this);
        findpsw.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tittle_common_cancel:
                finish();
                break;
            case R.id.findpsw:
                String email = findpsw_content.getText().toString().trim();
                if (checkEmail(email)) {
                    findPSW(email);
                }
                break;
            default:
                break;
        }
    }

    private void findPSW(String email) {
        RequestParams params = new RequestParams();
        params.put("email", email);
        final ProgressHUD ProgressDialog = ProgressHUD.show(this, "正在加载...", true, true, null);
        AddClient.get(Address.GETPASSWD, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, String content) {
                if (null != ProgressDialog && ProgressDialog.isShowing()) {
                    ProgressDialog.dismiss();
                }
                try {
                    UploadInfo info = JSON.parseObject(content, UploadInfo.class);
                    String result = info.getResult();
                    if (result.equals("0")) {
                        Toast.makeText(getApplicationContext(), "发送成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "发送失败,请检查邮箱", Toast.LENGTH_SHORT)
                                .show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                super.onSuccess(statusCode, content);
            }

            @Override
            public void onFailure(Throwable error, String content) {
                if (null != ProgressDialog && ProgressDialog.isShowing()) {
                    ProgressDialog.dismiss();
                }
                super.onFailure(error, content);
            }
        });

    }

    private boolean checkEmail(String email) {
        if (TextUtils.isEmpty(email)) {
            Toast.makeText(getApplicationContext(), "邮箱不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}
